/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.enterprise.context.RequestScoped;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;

/**
 * Ein Article spiegelt einen Beitrag im Wiki Wieder. In im werden die Verschiedenen Versionen Gespeichert und 
 * der Name des Beitrags welcher nicht Versioniert wird.+
 * 
 * @author Patrick Kuhlmann
 */
@RequestScoped
@Entity
public class Article implements Serializable{
    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String theme;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date date;
    @OneToMany (cascade = CascadeType.ALL)
    private List<Version> versions;
    
    public Version getLastVersion(){
        return versions.get(versions.size()-1);
    }
    
    public Post firstPost(){
        Version last = getLastVersion();
        Post post = last.getFirstPosts();
        if(post!=null){
            return post;
        }else{        
            return new Post("","","");
                }
    }
    public Article(String theme) {
        this.theme = theme;
        date = new Date();
    }

    public Article() {
    }

    public List<Version> getVersions() {
        return versions;
    }
    
    public Version getVersion(int id) {
        return versions.get(id);
    }

    public void setVersions(List<Version> versions) {
        this.versions = versions;
    }
    
    public void addVersions(Version v){
        if(versions!=null){
            versions.add(v);
        }else{
            versions=new ArrayList<>();
            
            versions.add(v);
        }
        
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTheme() {
        return theme;
    }

    public void setTheme(String theme) {
        this.theme = theme;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
    
    
}
